/*
 * Start.java
 *
 * Created on 2008年10月27日, 下午2:12
 */

package ipmsg;

import ipmsg.etc.Command;
import ipmsg.etc.GlobalConstant;
import ipmsg.etc.UtilityGlobal;
import ipmsg.etc.GlobalVar;
import ipmsg.etc.User;
import ipmsg.file.ConfigAddedUsers;
import ipmsg.file.ConfigBlacklist;
import ipmsg.file.Configuration;
import ipmsg.gui.AddedUsers;
import ipmsg.gui.Attack;
import ipmsg.gui.Blacklist;
import ipmsg.gui.MsgWindow;
import ipmsg.gui.ReadyToSendFiles;
import ipmsg.gui.SendFiles;
import ipmsg.gui.UtilityGui;
import ipmsg.network.FileDaemonServer;
import ipmsg.network.InterfaceFreshUsers_Start;
import ipmsg.network.MsgDaemonProcessor;
import ipmsg.network.MsgDaemonServer;
import ipmsg.network.UtilityNet;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author  Noeru
 */
public class Start extends javax.swing.JFrame implements 
        InterfaceFreshUsers_Start{

    /** Creates new form Start */
    public Start() {
        //可行性验证
        checkVersion();
        //初始化系统变量
        GlobalVar.init();
        //验证编码
        checkCharset();
        //开启后台线程
        Thread th;
        th=new Thread(new MsgDaemonProcessor(this));
        th.start();
        th=new Thread(new MsgDaemonServer());
        th.start();
        th=new Thread(new FileDaemonServer());
        th.start();
        
        //初始化图形界面
        initTheme();
        initComponents();
        freshGui(true);
        //广播登陆消息
        this.login();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        guiUserList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        GuiTips = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem5 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem15 = new javax.swing.JMenuItem();
        jMenuItem19 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem4 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem7 = new javax.swing.JRadioButtonMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jRadioButtonMenuItem5 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem6 = new javax.swing.JRadioButtonMenuItem();
        jMenuItem18 = new javax.swing.JMenuItem();
        jMenuItem17 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new java.awt.BorderLayout());

        guiUserList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                guiUserListMouseClicked(evt);
            }
        });
        guiUserList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                guiUserListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(guiUserList);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        GuiTips.setText(" ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(GuiTips, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addComponent(GuiTips)
                .addContainerGap())
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);

        jMenu1.setText("系统");

        jMenuItem1.setText("刷新好友列表");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("查看好友信息");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator1);

        jMenuItem3.setText("查看本地IP列表");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem4.setText("刷新本地IP信息");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);
        jMenu1.add(jSeparator2);

        jMenuItem5.setText("加入黑名单");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);
        jMenu1.add(jSeparator3);

        jMenuItem6.setText("关于");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuItem7.setText("退出");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem7);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("窗口");

        jMenuItem8.setText("发送消息");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem8);

        jMenuItem9.setText("广播消息");
        jMenu2.add(jMenuItem9);

        jMenuItem10.setText("发送文件");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem10);
        jMenu2.add(jSeparator4);

        jMenuItem11.setText("管理好友");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem11);

        jMenuItem12.setText("管理黑名单");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem12);

        jMenuItem13.setText("日志");
        jMenu2.add(jMenuItem13);

        jMenuItem15.setText("攻击");
        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem15ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem15);

        jMenuItem19.setText("发送列表");
        jMenuItem19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem19ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem19);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("配置");

        jMenuItem14.setText("修改用户昵称");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem14);

        jMenuItem16.setText("修改主机昵称");
        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem16ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem16);

        jMenu4.setText("发送消息方式");

        buttonGroup1.add(jRadioButtonMenuItem1);
        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("Ctrl+Enter");
        jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem1ActionPerformed(evt);
            }
        });
        jMenu4.add(jRadioButtonMenuItem1);

        buttonGroup1.add(jRadioButtonMenuItem2);
        jRadioButtonMenuItem2.setText("Entel");
        jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem2ActionPerformed(evt);
            }
        });
        jMenu4.add(jRadioButtonMenuItem2);

        jMenu3.add(jMenu4);

        jMenu5.setText("编码类型");

        buttonGroup2.add(jRadioButtonMenuItem3);
        jRadioButtonMenuItem3.setSelected(true);
        jRadioButtonMenuItem3.setText("GB2312(推荐)");
        jRadioButtonMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem3ActionPerformed(evt);
            }
        });
        jMenu5.add(jRadioButtonMenuItem3);

        buttonGroup2.add(jRadioButtonMenuItem4);
        jRadioButtonMenuItem4.setText("UTF8");
        jRadioButtonMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem4ActionPerformed(evt);
            }
        });
        jMenu5.add(jRadioButtonMenuItem4);

        buttonGroup2.add(jRadioButtonMenuItem7);
        jRadioButtonMenuItem7.setText("shift_jis(鸟日本的)");
        jRadioButtonMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem7ActionPerformed(evt);
            }
        });
        jMenu5.add(jRadioButtonMenuItem7);

        jMenu3.add(jMenu5);

        jMenu6.setText("主题类型");

        buttonGroup3.add(jRadioButtonMenuItem5);
        jRadioButtonMenuItem5.setSelected(true);
        jRadioButtonMenuItem5.setText("JAVA");
        jRadioButtonMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem5ActionPerformed(evt);
            }
        });
        jMenu6.add(jRadioButtonMenuItem5);

        buttonGroup3.add(jRadioButtonMenuItem6);
        jRadioButtonMenuItem6.setText("OS_BASED");
        jRadioButtonMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem6ActionPerformed(evt);
            }
        });
        jMenu6.add(jRadioButtonMenuItem6);

        jMenu3.add(jMenu6);

        jMenuItem18.setText("修改日志大小限制");
        jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem18ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem18);

        jMenuItem17.setText("重置");
        jMenuItem17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem17ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem17);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-278)/2, (screenSize.height-628)/2, 278, 628);
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
    this.exit();
}//GEN-LAST:event_formWindowClosing

private void guiUserListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_guiUserListValueChanged
// TODO add your handling code here:
    int selectLen=this.guiUserList.getSelectedIndices().length;
    if(selectLen==1)this.GuiTips.setText("ip  ： "+
            this.guiUserList.getSelectedValue());
    if(selectLen>1)this.GuiTips.setText("已选择 "+selectLen+" 个用户！" +
            "点击菜单\"窗口-聊天\"进行群聊！");
}//GEN-LAST:event_guiUserListValueChanged

private void guiUserListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_guiUserListMouseClicked
// TODO add your handling code here:
    if(evt.getClickCount()==2){ 
        if(!UtilityGui.newWindow(
                (String) this.guiUserList.getSelectedValue(),null,null))
            this.GuiTips.setText("窗口已打开");
        /*if(!UtilityMsgSendRecv.newWindow(
                (String) this.guiUserList.getSelectedValue(), null)){
            this.warning.setText("窗口已经打开！");
        }*/
    }
}//GEN-LAST:event_guiUserListMouseClicked

private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
// TODO add your handling code here:
    this.exit();
}//GEN-LAST:event_jMenuItem7ActionPerformed

private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
// TODO add your handling code here:
    UtilityGui.showNotice(this,"兼容飞鸽的LanMsg"+GlobalVar.NEW_LINE+
            "兼容GB2312和UTF8编码"+GlobalVar.NEW_LINE+
            "兼容Windows和Linux平台"+GlobalVar.NEW_LINE+
            "邱金武（qiujinwu456@gmail.com)"+GlobalVar.NEW_LINE+
            "为您服务!"
            );
}//GEN-LAST:event_jMenuItem6ActionPerformed

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
// TODO add your handling code here:
    StringBuffer sb=new StringBuffer();
    sb.append("共有 "+GlobalVar.ALL_IP_ADDRESS.size()+" 个ip"+
            GlobalVar.NEW_LINE);
    for(int i=0;i<GlobalVar.ALL_IP_ADDRESS.size();i++)
        sb.append(GlobalVar.ALL_IP_ADDRESS.get(i)+GlobalVar.NEW_LINE);
    UtilityGui.showNotice(this, sb.toString());
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
// TODO add your handling code here:
    UtilityNet.getMyIps();
}//GEN-LAST:event_jMenuItem4ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
// TODO add your handling code here:
    int selectLen;
    if ((selectLen=this.guiUserList.getSelectedIndices().length) < 1) {
        this.GuiTips.setText("请选择用户！");
        return;
    }
    else if(selectLen==1){
        User usr=GlobalVar.getUser((String) 
                this.guiUserList.getSelectedValue());
        UtilityGui.showNotice(this,"昵称 ： "+usr.getName()+GlobalVar.NEW_LINE
                +"ip : "+usr.getIp()+GlobalVar.NEW_LINE
                +"组别 ： "+usr.getGroup()+GlobalVar.NEW_LINE
                +"主机 ： "+usr.getHost());
    }else {
        Object[] o = this.guiUserList.getSelectedValues();
        User usr;
        StringBuffer sb=new StringBuffer();
        sb.append("昵称：ip：组别：主机"+GlobalVar.NEW_LINE);
        for(int i=0;i<o.length;i++){
            usr=GlobalVar.getUser((String)o[i]);
            sb.append(usr.getName()+" : "+usr.getIp()+" : "+
                    usr.getGroup()+" : "+usr.getHost()+GlobalVar.NEW_LINE);
        }
        UtilityGui.showNotice(this,sb.toString());
    }
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    GlobalVar.clearUsers();
    this.refreshUsers();
    this.login();
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem14ActionPerformed
// TODO add your handling code here:
    String str;
    if((str=UtilityGui.getInputString(this,"输入昵称", "修改昵称", 
            GlobalConstant.NAMELEN))==null){
        this.GuiTips.setText("昵称修改被取消或失败！");
        return;
    }
    GlobalVar.USER_NAME=str;
    this.setTitle("飞鸽(LanMsg)-"+GlobalVar.USER_NAME);
    this.broadcastPriInfo();
}//GEN-LAST:event_jMenuItem14ActionPerformed

private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem1ActionPerformed
// TODO add your handling code here:
    GlobalVar.SEND_MSG_STYLE=GlobalConstant.CTRL_ENTER;
}//GEN-LAST:event_jRadioButtonMenuItem1ActionPerformed

private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem2ActionPerformed
// TODO add your handling code here:
    GlobalVar.SEND_MSG_STYLE=GlobalConstant.ENTER;
}//GEN-LAST:event_jRadioButtonMenuItem2ActionPerformed

private void jRadioButtonMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem3ActionPerformed
// TODO add your handling code here:
    GlobalVar.CHARACTER_ENCODING="GB2312";
}//GEN-LAST:event_jRadioButtonMenuItem3ActionPerformed

private void jRadioButtonMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem4ActionPerformed
// TODO add your handling code here:
    GlobalVar.CHARACTER_ENCODING="UTF8";
}//GEN-LAST:event_jRadioButtonMenuItem4ActionPerformed

private void jRadioButtonMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem5ActionPerformed
// TODO add your handling code here:
    GlobalVar.THEME=GlobalConstant.JAVA;
    this.GuiTips.setText("主题生效需要重启程序！");
}//GEN-LAST:event_jRadioButtonMenuItem5ActionPerformed

private void jRadioButtonMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem6ActionPerformed
// TODO add your handling code here:
    GlobalVar.THEME=GlobalConstant.OS_BASED;
    this.GuiTips.setText("主题生效需要重启程序！");
}//GEN-LAST:event_jRadioButtonMenuItem6ActionPerformed

private void jMenuItem17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem17ActionPerformed
// TODO add your handling code here:

    if (!UtilityGui.confirm(this, "重置设置", "您确定要重置吗？")) {
        return;
    }
    GlobalVar.initDefault();
    this.broadcastPriInfo();
    UtilityGui.showNotice(this, "主题生效需要重启程序！");
    this.freshGui(false);
}//GEN-LAST:event_jMenuItem17ActionPerformed

private void jMenuItem18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem18ActionPerformed
// TODO add your handling code here:    
    this.GuiTips.setText("当前日志最大长度限制为"+GlobalVar.LOG_MAX_LEN+" (KB)");
    int maxSize;
    if((maxSize=UtilityGlobal.getNumber(
            UtilityGui.getInputString(this,
            "输入长度（请输入数字,单位：K）", "修改日志最大长度限制", 10)
            ))<0){
        this.GuiTips.setText("修改日志最大长度限制被取消或失败！");
        return;
    }
    GlobalVar.LOG_MAX_LEN=maxSize;
    this.GuiTips.setText("修改日志最大长度限制至"+maxSize+" (KB)");
}//GEN-LAST:event_jMenuItem18ActionPerformed

private void jMenuItem16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem16ActionPerformed
// TODO add your handling code here:
    String str;
    if((str=UtilityGui.getInputString(this,"输入主机名", "修改主机名称", 
            GlobalConstant.NAMELEN))==null){
        this.GuiTips.setText("主机名修改被取消或失败！");
        return;
    }
    GlobalVar.HOST_NAME=str;
    this.broadcastPriInfo();
}//GEN-LAST:event_jMenuItem16ActionPerformed

private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
// TODO add your handling code here:
    int selectLen;
    if ((selectLen=this.guiUserList.getSelectedIndices().length) < 1) {
        this.GuiTips.setText("请选择用户！");
        return;
    }
    if(selectLen==1){
        if(!UtilityGui.newWindow(
                (String) this.guiUserList.getSelectedValue(), null,null)){
            this.GuiTips.setText("窗口已经打开！");
        }
    }else{
        Object[] o = this.guiUserList.getSelectedValues(); 
        java.util.ArrayList<String> tmp=new java.util.ArrayList<String>();
        for(int i=0;i<o.length;i++){
            tmp.add((String)o[i]);
        }
        new MsgWindow(tmp).setVisible(true);
    }
}//GEN-LAST:event_jMenuItem8ActionPerformed

private void jRadioButtonMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem7ActionPerformed
// TODO add your handling code here:
    GlobalVar.CHARACTER_ENCODING="SHIFT_JIS";
}//GEN-LAST:event_jRadioButtonMenuItem7ActionPerformed

private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
// TODO add your handling code here:
    if(!GlobalVar.WINDOW_REG.contains("addedUser"))
        new AddedUsers().setVisible(true);
    else this.GuiTips.setText("窗口已打开");
}//GEN-LAST:event_jMenuItem11ActionPerformed

private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
// TODO add your handling code here:
    if(!GlobalVar.WINDOW_REG.contains("blacklist"))
        new Blacklist().setVisible(true);
    else this.GuiTips.setText("窗口已打开");
}//GEN-LAST:event_jMenuItem12ActionPerformed

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
// TODO add your handling code here:
    if (this.guiUserList.getSelectedIndices().length < 1) {
        this.GuiTips.setText("请选择用户！");
        return;
    }
    boolean change=false;
    Object[] o = this.guiUserList.getSelectedValues();
    for (int i = 0; i < o.length; i++) {
        if(GlobalVar.BLACK_LIST.contains((String)o[i]))continue;
        if(GlobalVar.ALL_IP_ADDRESS.contains((String)o[i]))continue;
        GlobalVar.BLACK_LIST.add((String)o[i]);
        GlobalVar.delUser((String)o[i]);
        change=true;
    }
    if(change)refreshUsers();
    
    
}//GEN-LAST:event_jMenuItem5ActionPerformed

private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem15ActionPerformed
// TODO add your handling code here:
    int selectLen;
    if ((selectLen = this.guiUserList.getSelectedIndices().length) < 1) {
        this.GuiTips.setText("请选择用户！");
        return;
    } else if (selectLen == 1) {
        if (!GlobalVar.WINDOW_REG.contains("attack")) {
            new Attack((String) 
                    this.guiUserList.getSelectedValue()).setVisible(true);
        }else {
            this.GuiTips.setText("群攻危险");
        }
    } else {
        this.GuiTips.setText("群攻危险！");
    }
}//GEN-LAST:event_jMenuItem15ActionPerformed

private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
// TODO add your handling code here:
    int selectLen;
    if ((selectLen=this.guiUserList.getSelectedIndices().length) < 1) {
        this.GuiTips.setText("请选择用户！");
        return;
    }
    else if(selectLen==1){
        new SendFiles((String) this.guiUserList.getSelectedValue()).setVisible(true);
    }
    else this.GuiTips.setText("不支持同时发送给多个用户！");
}//GEN-LAST:event_jMenuItem10ActionPerformed

private void jMenuItem19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem19ActionPerformed
// TODO add your handling code here:
    new ReadyToSendFiles().setVisible(true);
}//GEN-LAST:event_jMenuItem19ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Start().setVisible(true);
            }
        });
        /*FileLinkList TMP = FileLinkList.createFileLinkList(
                "0:list(200805).xls:0bf0d:48bab562:1:1:我的简历:00:48fdbf1d:2:2:list(200806).xls:010b28:48bab4c1:1:",
                "1.1.1.1");
        TMP.show();*/
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel GuiTips;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JList guiUserList;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem17;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem19;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem4;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem5;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem6;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    // End of variables declaration//GEN-END:variables

    private void exit(){
        
        if (!UtilityGui.confirm(this, "退出程序", "您确定要退出吗？")) {
            return;
        }
        Command tmpCom = new Command(GlobalConstant.IPMSG_BR_EXIT);
        tmpCom.setAdditional(GlobalVar.USER_NAME);
        UtilityNet.broadcastUdpPacket(tmpCom);
        Configuration.writeConfig();
        ConfigBlacklist.writeConfig();
        ConfigAddedUsers.writeConfig();
        System.exit(0);
    }
    
    private void login(){
        Command tmp=new Command(GlobalConstant.IPMSG_BR_ENTRY);
        tmp.setAdditional(GlobalVar.USER_NAME+'\0'+"LanMsg");//后面添加组名
        UtilityNet.broadcastUdpPacket(tmp);
        
    }
    
    private void freshGui(boolean boot){
        this.setTitle("飞鸽(LanMsg)-"+GlobalVar.USER_NAME);
        if(GlobalVar.SEND_MSG_STYLE==GlobalConstant.CTRL_ENTER)
            this.jRadioButtonMenuItem1.setSelected(true);
        else this.jRadioButtonMenuItem2.setSelected(true);
   
        if (GlobalVar.CHARACTER_ENCODING.equalsIgnoreCase("GB2312")) {
            this.jRadioButtonMenuItem3.setSelected(true);
        } else if (GlobalVar.CHARACTER_ENCODING.equalsIgnoreCase("UTF8")) {
            this.jRadioButtonMenuItem4.setSelected(true);
        } else this.jRadioButtonMenuItem7.setSelected(true);
        

        if(GlobalVar.THEME==GlobalConstant.JAVA)
            this.jRadioButtonMenuItem5.setSelected(true);
        else this.jRadioButtonMenuItem6.setSelected(true);
        
        if(!boot)this.broadcastPriInfo();

    }
    
    private void broadcastPriInfo(){
        Command com=new Command(GlobalConstant.IPMSG_BR_ABSENCE);
        com.setAdditional(GlobalVar.USER_NAME+'\0'+"LanMsg");
        UtilityNet.broadcastUdpPacket(com);
    }
    
    private void checkVersion(){
        //检测jdk
        if (System.getProperty("java.version").charAt(2) < '6') {
            UtilityGui.showNotice(this, "jdk 版本太低！");
            System.exit(1);
        }
        
        if(!UtilityNet.checkPort()){
            UtilityGui.showNotice(this, "程序可能已经被打开一次！");
            System.exit(1);
        }
    }
    
    private void checkCharset(){
        
        //检查编码支持
        try {
            new String("qiujinwu").getBytes(GlobalVar.CHARACTER_ENCODING);
        } catch (UnsupportedEncodingException ex) {

            UtilityGui.showNotice(this,
                    "您的jdk似乎不支持目前的编码 ：\"" +
                    GlobalVar.CHARACTER_ENCODING + "\"" +
                    GlobalVar.NEW_LINE +
                    "您可以通过将\"charset.jar\"拷贝到" +
                    GlobalVar.NEW_LINE + "目录\"" +
                    System.getProperty("java.home") +
                    GlobalVar.FILE_PATH_DELIMITER +
                    "lib\"解决该问题");
            System.exit(-1);
           
        }
    }
    
    private void initTheme(){
        if(GlobalVar.THEME==GlobalConstant.OS_BASED)
            try {
                javax.swing.UIManager.setLookAndFeel(
                        javax.swing.UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                this.GuiTips.setText("初始化主题失败！");
            }
    }
    
    //刷新好友列表
    public void refreshUsers() {
        listCellRenderer render=new listCellRenderer();
        guiUserList.setListData(GlobalVar.getUserList());
        guiUserList.setCellRenderer(render);
    }
    
    
    /**
     * 实现联系人ip和昵称的一一对应。
     */
    static class listCellRenderer extends javax.swing.DefaultListCellRenderer {

        @Override
        /**
         * 根据ip显示用户的昵称
         */
        public java.awt.Component getListCellRendererComponent(
                javax.swing.JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            /*if (GlobalVar.usersMap == null) {
                return this;
            }*/
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof String) {
                String userName = (String) 
                        GlobalVar.getUser((String)value).getName();
                if (userName != null) {
                    setText(userName);
                }
            }
            return this;
        }
    }

}
